<?php
class adjBateAction extends HomeAction {


	public function step2(){

		$this->display();
	}

	public function index(){
		$this->display();
	}

	//获取附近司机列表
	public function getSJ(){
		$sign=C("adjKey");	//合作商KEY

		$lat=$_POST['lat'];
		$lng=$_POST['lng'];
		$imei="imei";
		$md5=md5($lat.$lng."aidaijia");
		$url="http://p.aidaijia.com/pservice.aspx";
		$string="1201|".$lat."|".$lng."|".$sign."|".$imei."|".$md5;
		log::write("获取123周围司机:".$url.$string);
		$data=$this->curls($url,$string);

		if(substr($data,0,3)=="EX|"){
			log::write("获取附近司机列表出错：".$data);
			$this->ajaxReturn(false,"网络忙，请重试",0);
		}else{
			$data=json_decode($data,true);
			if(empty($_POST['phone'])){
				$this->ajaxReturn($data,"获取成功",1);
			}else{
				//服务端调用，获取最近司机创建订单
				return $data;
			}
		}
	}

	//获取司机评价列表
	public function getSJComment(){

		$sign=C("adjKey");	//合作商KEY
		$ucode=$_POST['ucode'];

		$imei="imei";
		$md5=md5($ucode."aidaijia");
		$url="http://p.aidaijia.com/pservice.aspx";
		$string="1207|".$sign."|".$ucode."|".$imei."|".$md5;
		$data=$this->curls($url,$string);

		if(substr($data,0,3)=="EX|"){
			log::write("获取司机评价出错".$data);
			$this->ajaxReturn(false,"网络忙，请重试",0);
		}else{
			$data=json_decode($data,true);
			$this->ajaxReturn($data,"获取成功",1);
		}
	}

	//创建去保养订单
	public function createOrder(){

		//查看是否已注册
		$U = M("user");
		$data['username'] = $_POST['phone'];
		$userInfo=$U->where($data)->field("user_id,open_id,username,nickname,realname,address")->find();
		$ua_id = 0;
	
		$_SESSION['user']=$userInfo;
		if (!$userInfo||empty($_SESSION['user'])) {
			//注册用户并插入数据
			$userInfo['create_time'] = time();
			$userInfo['tel'] = $_POST['phone'];
			$userInfo['nickname'] = $_POST['phone'];
			$userInfo['username'] = $_POST['phone'];
			$userInfo['password'] = md5("123456");
			$userInfo['realname']=$_POST['realname'];
			$userInfo['address']=$_POST['address'];
			if($_SESSION['open_id']){
				$userInfo['open_id'] = $_SESSION['open_id'];
			}
			$user_id = $U->add($userInfo); //注册用户
			if($user_id){
				$userInfo['user_id'] = $user_id;
				$_SESSION['user'] = $userInfo;              //session记录数据
			}
		}else{
			if(!empty($_SESSION['open_id'])){
				//绑定微信OPENID
				$dataUserInfo['open_id']=$_SESSION['open_id'];
			}
			$dataUserInfo['realname']=$_SESSION['order']['realname'];
			$res = $U->where("user_id=".$userInfo['user_id'])->save($dataUserInfo);

			$_SESSION['user'] = $userInfo;              //session记录数据
		}
	

		$dataOrder['order_num']=time().rand(100, 999);
		$dataOrder['user_id']=$_SESSION['user']['user_id'];
		$dataOrder['wx_id']=$_POST['wx_id'];

		$dataOrder['ua_id']=$ua_id;
		$dataOrder['create_time']=time();
		$dataOrder['service_price']=150;

		$dataOrder['type']=3;

		$dataOrder['is_quche']=1;
		$dataOrder['uo_type']=3;
		$dataOrder['status']=5;
		$dataOrder['source']=$_SESSION['order']['source'];
		$dataOrder['order_name']="小保养";
		$partOrder=M('order');
		$order_id=$partOrder->add($dataOrder);
		$_SESSION['order']['order_id']=$order_id;
	
		if($order_id!==false){
			return $order_id;
		}else{
			log::write("用户".$_SESSION['user']['username']."下单失败");
		}
	}

	//创建订单，获取订单号 2.10接口
	public function createOrderNow(){
		$orderIdQBY=$this->createOrder();	//创建订单

		$sign=C("adjKey");	//合作商KEY
		$orderId=$_POST['orderId'];

		$name=$_POST['name'];
		$phone=$_POST['phone'];
		$address=trim($_POST['address']);
		$lat=$_POST['lat'];
		$lng=$_POST['lng'];
		$imei="imei";
		$md5=md5($phone."aidaijia");

		$shiji=$this->getSJ();	//获取周围司机

		for($i=0;$i<count($shiji);$i++){
			//获取最近空闲司机
			if($shiji[$i]['state']=="空闲中"){
				$ucode=$shiji[$i]["ucode"];
				break;
			}
		}

		if(empty($ucode)){
			log::write("即时创建订单失败".$adj_id);
			$this->ajaxReturn(false,"很抱歉，周围司机都在服务中",0);
		}

		//$ucode="HZ2483";	//测试用

		$url="http://p.aidaijia.com/pservice.aspx";
		$string="1212|".$name."|".$phone."|".$address."|".$ucode."|".$lat."|".$lng."|8|".$sign."|imei|".$md5."|99qby";

		log::write("____".$url.$string);
		$adj_id=$this->curls($url,$string);

		if(substr($adj_id,0,3)=="EX|"){
			log::write("即时创建订单失败".$adj_id);
			$this->ajaxReturn(false,"网络忙，请重试",0);
		}else{
			$data['order_id']=$orderIdQBY;
			$data['adj_id']=$adj_id;
			$data['create_time']=time();
			$data['type']=1;
			$data['status']=0;
			$res=M("order_adj")->add($data);
			if($res){
				$this->ajaxReturn($adj_id,"创建订单成功",1);
			}else{
				log::write("创建爱代驾订单失败，ID:".$adj_id);
				$res=M('order')->where('dj_id='.$orderIdQBY)->delete();
				$this->ajaxReturn(false,"网络忙，请重试",0);
			}
		}
	}

	//获取爱代驾订单详情，接口号：2.11
	public function getOrderInfo(){
		$sign=C("adjKey");	//合作商KEY
		$orderId=$_POST['orderId'];

		//$orderId="JCHLKURGVREQ";
		
		$imei="imei";
		$md5=md5($orderId."aidaijia");
		$url="http://p.aidaijia.com/pservice.aspx";
		$string="1213|".$orderId."|".$sign."|".$imei."|".$md5;

		$data=$this->curls($url,$string);

		//$data='[{"Address":"\u676d\u5dde\u5e02 \u8427\u5c71\u533a \u8427\u676d\u8def 22-11\u53f7","AllMoney":0,"AllUserCount":1,"AppointTime":"2015-05-10 14:44","BusinessType":1,"CarNumber":null,"CellPhone":"17091608896","CreateUser":null,"CustomerName":"android APP\u65b0\u7528\u6237","DiscountMoney":0,"DriverLat":0,"DriverLng":0,"DriverPhone":"","FromCellPhone":"17091608896","Lat":0,"Lng":0,"Mileage":0,"MileageMoney":0,"OrderFrom":0,"OrderId":"JCHLKURGVREQ","OrderTime":"2015-05-10 14:44","Parterid":0,"PhoneCallTime":0,"RecommendCode":null,"Remark":"","SendUserId":0,"SendUserName":null,"Status":40,"Ucode":"HZ2483","WaitMoney":0,"WaitTime":0}]';
		log::write("获取订单详情".$orderId."结果：".$data);

		if(substr($data,0,3)=="EX|"){
			$this->ajaxReturn(false,"网络忙，请重试",0);
		}else{
			$data=json_decode($data,true);
			$this->updateOrderInfo($data[0]);
			$this->ajaxReturn($data,"获取订单成功",1);
		}
	}

	//更新订单数据
	public function updateOrderInfo($data){
		//$data['']=
	}


	//取消订单
	public function closeOrder(){
		$sign=C("adjKey");	//合作商KEY
		$orderId=$_POST['orderId'];
		//$orderId="HMMQWNSS2Z4U";
		$imei="imei";
		$md5=md5($orderId."aidaijia");
		$url="http://p.aidaijia.com/pservice.aspx";
		$string="1209|".$orderId."|".$sign."|".$md5;
		$data=$this->curls($url,$string);
		log::write("取消爱代驾订单".$orderId."结果：".$data);

		if(substr($data,0,3)=="EX|"||$data==0){
			$this->ajaxReturn(false,"网络忙，请重试",0);
		}else if($data==1){
			//获取订单号并取消订单
			$map['order_id']=M('order_adj')->where('adj_id='.$orderId)->getField("order_id");
			$res=M('order')->where($map)->setField("status",100);
			if($res){
				log::write("取消订单".$map['order_id']."结果：".$data);
			}

			$this->ajaxReturn($res,"取消订单成功",1);
			
		}
	}




	//地址转换为经纬度
	public function geocoder(){

		$url="http://api.map.baidu.com/geocoder/v2/?ak=".C("baiduMapKey");
		$url.="&output=json";	//规定返回数据格式
		$url.="&address=".urlencode($_POST['address']);

		$data=file_get_contents($url);
		$data=json_decode($data,true);
		if($data['status']==0){
			$this->ajaxReturn($data['result'],"地址转换成功",1);
		}else{
			$this->ajaxReturn("","无法获取定位，请重试！",0);
		}
	}


	//坐标转换API，把GPS坐标转换为百度坐标
	public function geoconv(){
		$location=$_POST["lat"].",".$_POST["lon"];
		$output="html";	//表示输出类型，web上必须指定为html才能展现地图产品结果。
		$from="1";
		$key=C("baiduMapKey");
		$url="http://api.map.baidu.com/geoconv/v1/?ak=".C("baiduMapKey");
		$url.="&coords=120.2534241,30.173119099999997&output=json";

		//$url=urlencode($url);
		//{"status":0,"result":[{"x":120.26434801937,"y":30.176881129349}]}

		echo $url;
		$data=file_get_contents($url);
		$data=json_decode($data,true);

		dump($data);

	}



	//与爱代驾交互用
	public function curls($url, $data_string) {
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_URL, $url);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	        'X-AjaxPro-Method:ShowList',
	        'Content-Type: application/json; charset=utf-8',
	        'Content-Length: ' . strlen($data_string))
	    );
	    curl_setopt($ch, CURLOPT_POST, 1);
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
	    $data = curl_exec($ch);
	    curl_close($ch);
	    return $data;
	}

    
}

?>